package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.tools.UserDao;
/**
 * 登录验证控制模块
 * @author 演示
 *
 */

public class LoginServlet extends BaseServlet {

private static final long serialVersionUID=1L;
	

	public void userLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		System.out.println("====================================================");
		String path = "login.jsp";
		String userid=request.getParameter("userid");
		String pwd =request.getParameter("pwd");
		System.out.println("================="+userid+":"+pwd);
		try{
			if (userid!=null&&!userid.trim().equals("")) {
				UserDao ud=new UserDao();
				User user = ud.find(Integer.valueOf(userid));
				if(user!=null && user.getPwd().equals(pwd)){
					request.getSession().setAttribute("user", user);
					response.sendRedirect(request.getContextPath() + "/index.jsp");
					return;
				}
				request.setAttribute("error","账号与密码不匹配");
				request.getRequestDispatcher("login.jsp").forward(request, response);
				return;
			}
			request.setAttribute("error","账号与密码不匹配");
			request.getRequestDispatcher("login.jsp").forward(request, response);
			return;
		}catch(Exception e){
			e.printStackTrace();
			request.getRequestDispatcher("error/500.jsp").forward(request, response);
			return;
		}
		
	}


}
